package com.woniuxy.house.admin;

import com.woniuxy.house.entity.PageBean;
import com.woniuxy.house.entity.Post;
import com.woniuxy.house.entity.Source;
import com.woniuxy.house.service.PostService;
import com.woniuxy.house.service.SourceService;
import com.woniuxy.house.utils.JwtUtil;
import com.woniuxy.house.vo.ResponseEntity;
import com.woniuxy.house.vo.SourceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 后台房源
 */
@RestController("adminsourcecontroller")
@RequestMapping("/source")
@Slf4j
public class SourceController {
    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private SourceService sourceService;
    //后台展示房源列表
    @RequestMapping("/list")
    public ResponseEntity list(@RequestHeader String jwt){
        if(jwtUtil.verifyJWT(jwt)){//如果校验为true
            //int UserId = (int)jwtUtil.verifyJWTGetUserId(jwt,"id");//从这里得到jwt中的roleId
            return  new ResponseEntity("200","成功",sourceService.getByPage(1));//初始化 默认查第一页数据
        }else{//如果检验失败 即为令牌无效
            return  new ResponseEntity("700","令牌失效请重新登录",null);

        }
    }

    @RequestMapping("/reset")
    public ResponseEntity reset(){//重置
        PageBean<Source> byPage = sourceService.getByPage(1);
        return  new ResponseEntity("200","成功",byPage);
    }

    @RequestMapping("/getpage/{page}")//后续的分页查询
    public ResponseEntity getPage(@PathVariable int page){
        PageBean<Source> byPage = sourceService.getByPage(page);
        return   new ResponseEntity("200","成功",byPage);
    }

    //后台执行修改操作
    @RequestMapping("/update")
    public ResponseEntity update(@RequestBody Source source){
        sourceService.update(source);
        return  ResponseEntity.SUCCESS;
    }

    //后台执行删除房源（改变房源出租状态）
    @RequestMapping("/delete")
    public ResponseEntity delete(@RequestBody Source source){
        sourceService.update(source);
        return ResponseEntity.SUCCESS;
    }


    //多条件查询房源
    @RequestMapping("/condition")
    public ResponseEntity getByCondition(@RequestBody SourceVo sourceVo){
        log.debug("source{}"+sourceVo.getSource());
        log.debug("page{}"+sourceVo.getPage());
        PageBean<Source> byCondition = sourceService.getByCondition(sourceVo.getSource(), sourceVo.getPage());
        return new ResponseEntity("200","成功",byCondition);
    }


}
